package com.ms.education.config;


import com.ms.education.listener.RequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * 注册拦截器
 * @author msjava
 * @date 2022/1/21 22:51
 */
@Configuration
public class InterseptorMvcConfig extends WebMvcConfigurerAdapter {


    /**
     *  将自定义的拦截器放入spring context 容器中
     *  因为自定义拦截器中注入了stringRedisTemplate
     *  这样可以保证注册拦截器时，stringRedisTemplate也已经存在spring容器中，
     *  避免在拦截器用到stringRedisTemplate时stringRedisTemplate为null
     * @return
     */
    @Bean
    public RequestInterceptor getRequestInterceptor(){
        return new RequestInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration addPathPatterns = registry.addInterceptor(getRequestInterceptor());
        addPathPatterns.excludePathPatterns("/swagger-ui.html");
        addPathPatterns.excludePathPatterns("/webjars/springfox-swagger-ui/**");
        addPathPatterns.excludePathPatterns("/ucenter/**/loginByNumber/**");
        addPathPatterns.excludePathPatterns("/ucenter/**/loginOut");
        addPathPatterns.excludePathPatterns("/swagger-resources/**");
        addPathPatterns.excludePathPatterns("/csrf");
        addPathPatterns.excludePathPatterns("/");
        addPathPatterns.excludePathPatterns("/error");
        addPathPatterns.addPathPatterns("/**");
    }
}
